/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package descent;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import descent.Packet.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;

/**
 *
 * @author jimmy
 */
public class NetworkListener extends Listener {
private Client client;
private Descent descent;
    @Override
    public void connected(Connection cnctn) {
        Log.info("[CLIENT]you has connected");
        client.sendTCP(new PacketLoginRequest());
        
    }


    @Override
    public void disconnected(Connection cnctn) {
        Log.info("[CLIENT]you has disconnected");
    }


    @Override
    public void received(Connection cnctn, Object o) {
        
        if(o instanceof PacketLoginAccepted){
            boolean answer = ((PacketLoginAccepted) o).accepted;
            
            if (answer){
                
            }
            else{
                cnctn.close();
            }
        
        }
        
        if (o instanceof PacketMessage){
            String mess = ((PacketMessage)o).message;
            //descent.queMessage(mess);
            descent.messages.add(mess);
        }
        
        
        
    }

    void init(Client client, Descent d) {
        this.client = client;
        this.descent = d;
    }
    

    
}